package com.andnnl.queue;

import java.math.BigDecimal;

/**
 * Created by chenss on 2024/4/3.
 */
public class SimpleBlockingQueueTest {
    static {
        System.out.println("SimpleBlockingQueueTest");
        float cost = 0.0f;
        //如果是省网，就取省网价格
        if(cost==0) {
            System.out.println("SimpleBlockingQueueTest0");
        }
    }
    public static void main(String[] args) throws InterruptedException {
        SimpleBlockingQueue<Integer> queue = new SimpleBlockingQueue<>(10);

        // 创建生产者线程
        Thread producer = new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                try {
                    queue.put(i);
                    System.out.println("Producer put: " + i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 创建消费者线程
        Thread consumer = new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                try {
                    int item = queue.take();
                    System.out.println("    Consumer take: " + item);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 启动生产者和消费者线程
        producer.start();
        Thread.sleep(10);
        consumer.start();

        // 等待生产者和消费者线程执行完毕
        try {
            producer.join();
            consumer.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
